package main

import (
	"Ljohn/netTest/grpcTest/pb"
	rpb "Ljohn/netTest/grpcTest/proto"
	"bytes"
	"context"
	"fmt"
	"google.golang.org/grpc"
	"google.golang.org/grpc/reflection"
	"net"
)

type server struct {
}

func (m *server) Echo(ctx context.Context, in *pb.Empty) (out *pb.StringMessage, err error) {
	fmt.Println("这是一个输出请求")
	x1 := &pb.StringSingle{Id: "1", Name: "T1"}
	x2 := &pb.StringSingle{Id: "2", Name: "T2"}
	out = new(pb.StringMessage)
	out.Ss = append(out.Ss, x1)
	out.Ss = append(out.Ss, x2)
	return out, err
}

func (m *server) EchoContent(ctx context.Context, in *pb.ContentX) (out *pb.StringMessage, err error) {
	fmt.Println("这是一次有内容的请求")
	fmt.Println("-------------->", ctx)
	fmt.Println("Content: ", in.Hi)
	buf := bytes.Buffer{}
	buf.WriteString("Hi ")
	buf.WriteString(in.Hi)
	x1 := &pb.StringSingle{Id: "1", Name: buf.String()}
	out = new(pb.StringMessage)
	out.Ss = append(out.Ss, x1)
	return
}

func main() {
	listen, err := net.Listen("tcp", ":1011")
	if err != nil {
		fmt.Println("occur Err :", err)
		return
	}
	gServer := grpc.NewServer()
	pb.RegisterTestServer(gServer, &server{})
	reflection.Register(gServer)
	err = gServer.Serve(listen)
	if err != nil {
		fmt.Println("occur Err :", err)
		return
	}
}

type TestServer struct {
}

func (m *TestServer) ClientStream(req *rpb.StreamReq, resp rpb.HiStream_ClientStreamServer) (err error) {
	return
}
